package br.com.ridea.pegadacultural.utilidades;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MySQLBackup {

	private static String SEPARATOR = File.separator;

	/* PATH DA PASTA BIN DO MYSQL */
	private static String MYSQL_PATH = "C:" + SEPARATOR +"Program Files" + SEPARATOR +"MySQL" + SEPARATOR +"MySQL Server 5.6" + SEPARATOR +"bin" + SEPARATOR;


	/*
	 * NOME DA DATABASE
	 */
	private static String DATABASE = "docekuka";

	private List<String> dbList = new ArrayList<String>();

	public MySQLBackup() {

		String command = MYSQL_PATH + "mysqldump.exe";
		
		dbList.add(DATABASE);
		
		System.out.println("Iniciando backup...\n\n");

		int i = 1;

		long time1, time2, time;

		time1 = System.currentTimeMillis();

		for (String dbName : dbList) {
			ProcessBuilder pb = new ProcessBuilder(command, "--user=root","--password=root", dbName, 
							"--result-file=" + "C:"+SEPARATOR+"Backup" + SEPARATOR + dbName	+"_"+ TreatDate.formatDateIfen(new Date())+ ".sql");

			try {
				System.out.println("Backup do banco de dados (" + i + "): "	+ dbName + " ...");
				pb.start();
			} catch (Exception e) {

				e.printStackTrace();

			}

			i++;

		}
		time2 = System.currentTimeMillis();
		time = time2 - time1;
		System.out.println("\nBackups realizados com sucesso.\n\n");
		System.out.println("Tempo total de processamento: " + time + " ms\n");
		System.out.println("Finalizando...");

		try {
			Thread.sleep(2000);
		}catch (Exception e) {
		}

		System.out.println("Finalizado...");

	}

	public static void main(String[] args) {

//		MySQLBackup app = new MySQLBackup();

	}

}
